package com.hk.Mgr.web.controller.product;

import com.hk.Mgr.web.dto.ProductStaticInfoCacheDTO;
import com.hk.app.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.base.ResponseBase;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.service.product.IProductInfo;

@Controller
@RequestMapping(value = "/ph5")
public class ProductCreateH5 {

    private Logger logger = LoggerFactory.getLogger(ProductCreateH5.class);


    @Autowired
    private IProductInfo productInfoService;

    @ResponseBody
    @RequestMapping(value = "/h5")
    public ResponseBase<String> pIdTocreateH5(String id, String type, String key) {
        logger.info("ProductCreateH5 入参为 id ={}, type={}, key={}", id, type, key);
        ResponseBase<String> rb = new ResponseBase<>();
        String url = "";

        ProductStaticInfoCacheDTO productCache = readCacheFromRedis(id);

        Integer buyChannel = productCache.getBuyChannel();

        //兼容老版本注释以下代码
        /*
		if(StringHelper.isEmpty(key)){
			url = "/mobile/showByGoldError.html";
			rb.setResult(url);
			return rb;
		}
		
		*/
        /**
         *  1. 固定收益类
         *  url = "/mobile/fixedIncome.html?id=";
         *
         *
         *  2.固定收益类
         *
         *  3.浮动收益类
         *  url = "/mobile/fixedProType3.html?id=";
         *
         *  4.固定 + 浮动收益类
         *
         *  5.海外保险类
         */
        switch (type) {
            case "1":
                url = "/mobile/fixedIncome.html?id=" + id + "&buyChannel=" + buyChannel + "&time=" + System.currentTimeMillis();
                rb.setResult(url);
                break;

            case "3":
                url = "/mobile/fixedProType3.html?id=" + id + "&buyChannel=" + buyChannel + "&time=" + System.currentTimeMillis();
                rb.setResult(url);
                break;

            case "5":
                url = "/mobile/overseaSafe.html?id=" + id + "&buyChannel=" + buyChannel + "&time=" + System.currentTimeMillis();
                rb.setResult(url);
                break;

            default:
                logger.error("not support type");
        }

        return rb;
    }

    private ProductStaticInfoCacheDTO readCacheFromRedis(String id) {
        Long productId = Long.valueOf(id);

        ProductStaticInfoCacheDTO result = RedisUtils.getProductCache(productId);

        if(result == null) {
            ProductInfo prodInfo = productInfoService.findById(productId);
            result = RedisUtils.setProductCache(prodInfo);
        }

        return result;
    }

}
